﻿#pragma strict
import System.Collections.Generic;

var renderers:Renderer[];

var outlineMat:Material;
var outlineColorName:String = "_OutlineColor";
var outlineColor:Color = Color.red;
var outlineWidthName:String = "_OutlineWidth";
var outlineWidth:float = 0.02;
var isShow:boolean;

var isTest:boolean;
//每个renderer对应outlineMat槽,每个renderer都可能会加入outlineMat.
var lastMats:Material[];

function Start () {
	renderers = GetComponentsInChildren.<Renderer>();
	lastMats = new Material[renderers.length];
}

function Update () {
	if(isTest){
		isTest = false;
		
		Show();
	}
}

function Show(){
	for(var i:int = 0 ; i < renderers.length ; i++){
		var r:Renderer = renderers[i];
		if(r){
			//保存全部的material
			var mats:List.<Material> = new List.<Material>();
				mats.AddRange(r.sharedMaterials);
			
			if(isShow){
				if(lastMats[i] == null){
					mats.Add(outlineMat);
					r.sharedMaterials = mats.ToArray();
					
					lastMats[i] = r.sharedMaterials[r.sharedMaterials.length - 1];
					lastMats[i].SetColor(outlineColorName,outlineColor);
					lastMats[i].SetFloat(outlineWidthName,outlineWidth);
				}
			}else{
				if(lastMats[i] != null){
					mats.Remove(outlineMat);
					r.sharedMaterials = mats.ToArray();
					
					lastMats[i] = null;
				}
			}
		}
	}
}